package org.dromara.commission.service.impl;

import org.dromara.commission.domain.vo.LzshHandworkCommissionImportVo;
import org.dromara.commission.mapper.LzshHandworkCommissionVoMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.commission.domain.bo.LzshHandworkCommissionBo;
import org.dromara.commission.domain.vo.LzshHandworkCommissionVo;
import org.dromara.commission.domain.LzshHandworkCommission;
import org.dromara.commission.mapper.LzshHandworkCommissionMapper;
import org.dromara.commission.service.ILzshHandworkCommissionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 手工佣金Service业务层处理
 *
 * @author Lion Li
 * @date 2024-07-25
 */
@RequiredArgsConstructor
@Service
public class LzshHandworkCommissionServiceImpl implements ILzshHandworkCommissionService {

    private final LzshHandworkCommissionMapper baseMapper;

    private final LzshHandworkCommissionVoMapper vobaseMapper;
    /**
     * 查询手工佣金
     *
     * @param id 主键
     * @return 手工佣金
     */
    @Override
    public LzshHandworkCommissionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询手工佣金
     *
     * @param businessCode 渠道视图_经营主体编码
     * @return 手工佣金
     */
    @Override
    public LzshHandworkCommissionVo queryByBusinessCode(String businessCode, String agentName, String accountPeriod) {
        return baseMapper.selectVoByBusinessCode(businessCode, agentName, accountPeriod);
    }

    /**
     * 分页查询手工佣金列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 手工佣金分页列表
     */
    @Override
    public TableDataInfo<LzshHandworkCommissionVo> queryPageList(LzshHandworkCommissionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LzshHandworkCommission> lqw = buildQueryWrapper(bo);
        Page<LzshHandworkCommissionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的手工佣金列表
     *
     * @param bo 查询条件
     * @return 手工佣金列表
     */
    @Override
    public List<LzshHandworkCommissionVo> queryList(LzshHandworkCommissionBo bo) {
        LambdaQueryWrapper<LzshHandworkCommission> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LzshHandworkCommission> buildQueryWrapper(LzshHandworkCommissionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LzshHandworkCommission> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getAccountPeriod()), LzshHandworkCommission::getAccountPeriod, bo.getAccountPeriod());
        lqw.like(StringUtils.isNotBlank(bo.getBusinessCode()), LzshHandworkCommission::getBusinessCode, bo.getBusinessCode());
        lqw.like(StringUtils.isNotBlank(bo.getAgentName()), LzshHandworkCommission::getAgentName, bo.getAgentName());
        lqw.like(StringUtils.isNotBlank(bo.getChannel()), LzshHandworkCommission::getChannel, bo.getChannel());
        lqw.like(StringUtils.isNotBlank(bo.getShopCode()), LzshHandworkCommission::getShopCode, bo.getShopCode());
        lqw.like(StringUtils.isNotBlank(bo.getNetworkName()), LzshHandworkCommission::getNetworkName, bo.getNetworkName());
        lqw.like(StringUtils.isNotBlank(bo.getReason()), LzshHandworkCommission::getReason, bo.getReason());
        lqw.eq(bo.getReward() != null, LzshHandworkCommission::getReward, bo.getReward());
        return lqw;
    }

    /**
     * 新增手工佣金
     *
     * @param bo 手工佣金
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(LzshHandworkCommissionBo bo) {
        LzshHandworkCommission add = MapstructUtils.convert(bo, LzshHandworkCommission.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改手工佣金
     *
     * @param bo 手工佣金
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(LzshHandworkCommissionBo bo) {
        LzshHandworkCommission update = MapstructUtils.convert(bo, LzshHandworkCommission.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LzshHandworkCommission entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除手工佣金信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public boolean insertBatch(List<LzshHandworkCommissionImportVo> list, int num){
        return vobaseMapper.insertBatch(list, num);
    }

    @Override
    public Boolean deleteByBcAndAn(String businessCode, String agentName) {
        return baseMapper.deleteByBcAndAn(businessCode, agentName);
    }

    @Override
    public boolean creatIndex() {
        try {
            baseMapper.creatIndex();
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
